﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using Model;//Please add references
namespace DAL
{
	/// <summary>
	/// 数据访问类:CUST_FANS
	/// </summary>
	public partial class CUST_FANSDAO
	{
		public CUST_FANSDAO()
		{}
		#region  BasicMethod

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CUST_FANS GetModel(string FromUserName)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from CUST_FANS ");
            strSql.Append(" where FromUserName=@FromUserName   ");  //在职人员
            SqlParameter[] parameters = {
                    new SqlParameter("@FromUserName", SqlDbType.VarChar,150)};
            parameters[0].Value = FromUserName;
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
                //return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
           
        }



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(CUST_FANS model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into CUST_FANS(");
			strSql.Append("NAME,GENDER,REGION,FROMUSERNAME,IMAGE,STATUS,REMARK,NOTICE_DATE,CANCEL_DATE,LAST_CONN_DATE,CREATE_DATE,CREATE_USER,LAST_MODI_DATE,LAST_MODI_USER)");
			strSql.Append(" values (");
			strSql.Append("@NAME,@GENDER,@REGION,@FROMUSERNAME,@IMAGE,@STATUS,@REMARK,@NOTICE_DATE,@CANCEL_DATE,@LAST_CONN_DATE,@CREATE_DATE,@CREATE_USER,@LAST_MODI_DATE,@LAST_MODI_USER)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@NAME", SqlDbType.VarChar,50),
					new SqlParameter("@GENDER", SqlDbType.Bit,1),
					new SqlParameter("@REGION", SqlDbType.VarChar,50),
					new SqlParameter("@FROMUSERNAME", SqlDbType.VarChar,150),
					new SqlParameter("@IMAGE", SqlDbType.VarChar,256),
					new SqlParameter("@STATUS", SqlDbType.Int,4),
					new SqlParameter("@REMARK", SqlDbType.VarChar,500),
					new SqlParameter("@NOTICE_DATE", SqlDbType.DateTime),
					new SqlParameter("@CANCEL_DATE", SqlDbType.DateTime),
					new SqlParameter("@LAST_CONN_DATE", SqlDbType.DateTime),
					new SqlParameter("@CREATE_DATE", SqlDbType.DateTime),
					new SqlParameter("@CREATE_USER", SqlDbType.VarChar,20),
					new SqlParameter("@LAST_MODI_DATE", SqlDbType.DateTime),
					new SqlParameter("@LAST_MODI_USER", SqlDbType.VarChar,20)};
			parameters[0].Value = model.NAME;
			parameters[1].Value = model.GENDER;
			parameters[2].Value = model.REGION;
			parameters[3].Value = model.FROMUSERNAME;
			parameters[4].Value = model.IMAGE;
			parameters[5].Value = model.STATUS;
			parameters[6].Value = model.REMARK;
			parameters[7].Value = model.NOTICE_DATE;
			parameters[8].Value = model.CANCEL_DATE;
			parameters[9].Value = model.LAST_CONN_DATE;
			parameters[10].Value = model.CREATE_DATE;
			parameters[11].Value = model.CREATE_USER;
			parameters[12].Value = model.LAST_MODI_DATE;
			parameters[13].Value = model.LAST_MODI_USER;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(CUST_FANS model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update CUST_FANS set ");
			strSql.Append("NAME=@NAME,");
			strSql.Append("GENDER=@GENDER,");
			strSql.Append("REGION=@REGION,");
			strSql.Append("FROMUSERNAME=@FROMUSERNAME,");
			strSql.Append("IMAGE=@IMAGE,");
			strSql.Append("STATUS=@STATUS,");
			strSql.Append("REMARK=@REMARK,");
			strSql.Append("NOTICE_DATE=@NOTICE_DATE,");
			strSql.Append("CANCEL_DATE=@CANCEL_DATE,");
			strSql.Append("LAST_CONN_DATE=@LAST_CONN_DATE,");
			strSql.Append("CREATE_DATE=@CREATE_DATE,");
			strSql.Append("CREATE_USER=@CREATE_USER,");
			strSql.Append("LAST_MODI_DATE=@LAST_MODI_DATE,");
			strSql.Append("LAST_MODI_USER=@LAST_MODI_USER");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@NAME", SqlDbType.VarChar,50),
					new SqlParameter("@GENDER", SqlDbType.Bit,1),
					new SqlParameter("@REGION", SqlDbType.VarChar,50),
					new SqlParameter("@FROMUSERNAME", SqlDbType.VarChar,150),
					new SqlParameter("@IMAGE", SqlDbType.VarChar,256),
					new SqlParameter("@STATUS", SqlDbType.Int,4),
					new SqlParameter("@REMARK", SqlDbType.VarChar,500),
					new SqlParameter("@NOTICE_DATE", SqlDbType.DateTime),
					new SqlParameter("@CANCEL_DATE", SqlDbType.DateTime),
					new SqlParameter("@LAST_CONN_DATE", SqlDbType.DateTime),
					new SqlParameter("@CREATE_DATE", SqlDbType.DateTime),
					new SqlParameter("@CREATE_USER", SqlDbType.VarChar,20),
					new SqlParameter("@LAST_MODI_DATE", SqlDbType.DateTime),
					new SqlParameter("@LAST_MODI_USER", SqlDbType.VarChar,20),
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = model.NAME;
			parameters[1].Value = model.GENDER;
			parameters[2].Value = model.REGION;
			parameters[3].Value = model.FROMUSERNAME;
			parameters[4].Value = model.IMAGE;
			parameters[5].Value = model.STATUS;
			parameters[6].Value = model.REMARK;
			parameters[7].Value = model.NOTICE_DATE;
			parameters[8].Value = model.CANCEL_DATE;
			parameters[9].Value = model.LAST_CONN_DATE;
			parameters[10].Value = model.CREATE_DATE;
			parameters[11].Value = model.CREATE_USER;
			parameters[12].Value = model.LAST_MODI_DATE;
			parameters[13].Value = model.LAST_MODI_USER;
			parameters[14].Value = model.ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from CUST_FANS ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from CUST_FANS ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public CUST_FANS GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ID,NAME,GENDER,REGION,FROMUSERNAME,IMAGE,STATUS,REMARK,NOTICE_DATE,CANCEL_DATE,LAST_CONN_DATE,CREATE_DATE,CREATE_USER,LAST_MODI_DATE,LAST_MODI_USER from CUST_FANS ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			CUST_FANS model=new CUST_FANS();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public CUST_FANS DataRowToModel(DataRow row)
		{
			CUST_FANS model=new CUST_FANS();
			if (row != null)
			{
				if(row["ID"]!=null && row["ID"].ToString()!="")
				{
					model.ID=int.Parse(row["ID"].ToString());
				}
				if(row["NAME"]!=null)
				{
					model.NAME=row["NAME"].ToString();
				}
				if(row["GENDER"]!=null && row["GENDER"].ToString()!="")
				{
					if((row["GENDER"].ToString()=="1")||(row["GENDER"].ToString().ToLower()=="true"))
					{
						model.GENDER=true;
					}
					else
					{
						model.GENDER=false;
					}
				}
				if(row["REGION"]!=null)
				{
					model.REGION=row["REGION"].ToString();
				}
				if(row["FROMUSERNAME"]!=null)
				{
					model.FROMUSERNAME=row["FROMUSERNAME"].ToString();
				}
				if(row["IMAGE"]!=null)
				{
					model.IMAGE=row["IMAGE"].ToString();
				}
				if(row["STATUS"]!=null && row["STATUS"].ToString()!="")
				{
					model.STATUS=int.Parse(row["STATUS"].ToString());
				}
				if(row["REMARK"]!=null)
				{
					model.REMARK=row["REMARK"].ToString();
				}
				if(row["NOTICE_DATE"]!=null && row["NOTICE_DATE"].ToString()!="")
				{
					model.NOTICE_DATE=DateTime.Parse(row["NOTICE_DATE"].ToString());
				}
				if(row["CANCEL_DATE"]!=null && row["CANCEL_DATE"].ToString()!="")
				{
					model.CANCEL_DATE=DateTime.Parse(row["CANCEL_DATE"].ToString());
				}
				if(row["LAST_CONN_DATE"]!=null && row["LAST_CONN_DATE"].ToString()!="")
				{
					model.LAST_CONN_DATE=DateTime.Parse(row["LAST_CONN_DATE"].ToString());
				}
				if(row["CREATE_DATE"]!=null && row["CREATE_DATE"].ToString()!="")
				{
					model.CREATE_DATE=DateTime.Parse(row["CREATE_DATE"].ToString());
				}
				if(row["CREATE_USER"]!=null)
				{
					model.CREATE_USER=row["CREATE_USER"].ToString();
				}
				if(row["LAST_MODI_DATE"]!=null && row["LAST_MODI_DATE"].ToString()!="")
				{
					model.LAST_MODI_DATE=DateTime.Parse(row["LAST_MODI_DATE"].ToString());
				}
				if(row["LAST_MODI_USER"]!=null)
				{
					model.LAST_MODI_USER=row["LAST_MODI_USER"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,NAME,GENDER,REGION,FROMUSERNAME,IMAGE,STATUS,REMARK,NOTICE_DATE,CANCEL_DATE,LAST_CONN_DATE,CREATE_DATE,CREATE_USER,LAST_MODI_DATE,LAST_MODI_USER ");
			strSql.Append(" FROM CUST_FANS ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ID,NAME,GENDER,REGION,FROMUSERNAME,IMAGE,STATUS,REMARK,NOTICE_DATE,CANCEL_DATE,LAST_CONN_DATE,CREATE_DATE,CREATE_USER,LAST_MODI_DATE,LAST_MODI_USER ");
			strSql.Append(" FROM CUST_FANS ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM CUST_FANS ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
			strSql.Append(")AS Row, T.*  from CUST_FANS T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "CUST_FANS";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

